﻿#include "VAOneXMLAdaptorPhysicalPropertyUniform.h"

#include "Tools/Win64/rapidxml/rapidxml.hpp"
#include "FITKAcousticsPhysics/FITKPhyscisPropertyUniform.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractMaterial.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"

#include <QDebug>

namespace IO
{

    bool VAOneXMLAdaptorPhysicalPropertyUniform::adaptR()
    {
        //读取均质板信息
        return readUniform();
    }

    bool VAOneXMLAdaptorPhysicalPropertyUniform::adaptW()
    {
        return writeUniform();
    }

    bool VAOneXMLAdaptorPhysicalPropertyUniform::readUniform()
    {
        if(!_node)
        {
            return false;
        }

        //转换类型为梁
        Acoustics::FITKPhyscisPropertyUniform* uniform = dynamic_cast<Acoustics::FITKPhyscisPropertyUniform*>(_dataObj);

        if(!uniform)
        {
            return false;
        }

        bool b = false;
        //获取名称节点
        auto nameNode = _node->first_node("Name");

        if (nameNode == nullptr)
        {
            //材料数据中 名称为必要项 不可为空 并抛出异常日志
            consoleMessage(2, "PropertyUniform name is null");
            return false;
        }

        QString strName = readStrNodeValue(nameNode, &b);

        if(b)
        {
            uniform->setDataObjectName(strName);//名称可以为空，但是节点不能为空
        }

        double dValue = readDoubleAttribute(_node, "thickness", &b);

        if(b)
        {
            uniform->setThickness(dValue);
        }

        //获取材料节点
        QString materialValue = readStrAttribute(_node, "material", &b);

        if(b)
        {
            //根据名称查找材料id，无则返回-1
            int materialID = getMaterialObjectIdByName(materialValue);
            uniform->setMaterialID(materialID);
        }
        else
        {
            //读材料读取异常
            consoleMessage(3,"Read Material Error" + materialValue);
        }

        //获取备注节点
        auto noteNode = _node->first_node("Note");

        if (noteNode != nullptr)
        {
            QString strNote = readStrNodeValue(noteNode, &b);
            if(b)
            {
                uniform->setDescribe(strNote);
            }
        }

        //获取颜色节点
        auto colorNode = _node->first_node("Color");
        float colorR = 0; float colorG = 0; float colorB = 0;
        //根据节点获取颜色
        readColorNodeValue(colorNode, colorR, colorG, colorB);
        //设置颜色
        uniform->setColor(colorR, colorG, colorB);
        return true;
    }

    bool VAOneXMLAdaptorPhysicalPropertyUniform::writeUniform()
    {
        if (!_node)// 判断节点是否存在
        {
            return false;
        }

        //转换类型
        Acoustics::FITKPhyscisPropertyUniform* data = dynamic_cast<Acoustics::FITKPhyscisPropertyUniform*>(_dataObj);

        if(!data)
        {
            return false;
        }

        if(!_node)
        {
            return false;
        }
        // 创建名称节点
        createNodeElement(_node, "Name", data->getDataObjectName());
        // 创建备注节点
        createNodeElement(_node, "Note", data->getDescribe());

        //创建颜色节点
        auto colorNode = createNodeElement(_node, "Color");

        //写入颜色节点信息
        if(colorNode)
        {
            float rgb[3] = {0.1f, 0.3f, 0.4f};
            data->getColor(rgb);
            writeAttribute(colorNode, "colorR", rgb[0]);
            writeAttribute(colorNode, "colorG", rgb[1]);
            writeAttribute(colorNode, "colorB", rgb[2]);
        }

        //写如属性值
        writeAttribute(_node, "thickness", data->getThickness());
        writeAttribute(_node, "material", getMaterialObjectNameById(data->getMaterialID()));
        return true;
    }
}
